from sqlalchemy import Column, Integer, String, BigInteger, DateTime, Text, Float
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
import enum

Base = declarative_base()

class TaskStatus(enum.Enum):
    """任务状态枚举"""
    PENDING = "pending"        # 等待处理
    PROCESSING = "processing"  # 处理中
    COMPLETED = "completed"    # 完成
    FAILED = "failed"         # 失败
    CANCELLED = "cancelled"    # 取消

class VideoTask(Base):
    """视频处理任务表"""
    __tablename__ = "video_tasks"
    
    id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
    task_id = Column(String(64), unique=True, nullable=False, comment="任务唯一标识")
    user_openid = Column(String(128), nullable=True, comment="用户OpenID")
    
    # 文件信息
    original_filename = Column(String(255), nullable=False, comment="原始文件名")
    video_url = Column(String(512), nullable=True, comment="视频文件URL")
    audio_url = Column(String(512), nullable=True, comment="音频文件URL")
    
    # 文件属性
    file_size = Column(BigInteger, nullable=True, comment="文件大小(字节)")
    duration = Column(Float, nullable=True, comment="视频时长(秒)")
    video_format = Column(String(32), nullable=True, comment="视频格式")
    audio_format = Column(String(32), nullable=True, comment="音频格式")
    
    # 任务状态
    status = Column(String(32), nullable=False, default=TaskStatus.PENDING.value, comment="任务状态")
    progress = Column(Integer, default=0, comment="处理进度(0-100)")
    error_message = Column(Text, nullable=True, comment="错误信息")
    
    # 时间戳
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    started_at = Column(DateTime, nullable=True, comment="开始处理时间")
    completed_at = Column(DateTime, nullable=True, comment="完成时间")
    
    def __repr__(self):
        return f"<VideoTask(id={self.id}, task_id='{self.task_id}', status='{self.status}')>" 